package temp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Janson
 * @Date 2022/2/13 15:20
 * @Version 1.0
 */
public class SingleNumber {
    public static void main(String[] args) {
//        int[] nums = new int[]{2,2,1};
        int[] nums2 = new int[]{4,1,2,1,2,3};

        int single = 0;
        for (int i=0;i< nums2.length;i++){
            single = single ^ nums2[i];
        }
        System.out.println("按位异或方式实现：" + single);
        List mapSingle = mapSingle(nums2);
        System.out.println("map字典实现："+mapSingle);
    }
    //如果出现两个次数为 1 的数字，直接用map字典，按位异或暂时没法用
    private static List mapSingle(int[] nums){
        ArrayList<Object> singleNum = new ArrayList<>();
        Map<Integer, Integer> single = new HashMap<>();
        for (int i=0;i<nums.length;i++){
            single.put(nums[i],single.getOrDefault(nums[i],0)+1);
        }
        for (int reuslt: single.keySet()) {
            if (single.get(reuslt)==1)
                singleNum.add(reuslt);
        }
        return singleNum;
    }
}
